package com.fs.groupSeven.service.impl;


import com.fs.groupSeven.Dao.UserDao;
import com.fs.groupSeven.Dao.UserRoleDao;
import com.fs.groupSeven.pojo.Role;
import com.fs.groupSeven.pojo.User;
import com.fs.groupSeven.pojo.UserRole;
import com.fs.groupSeven.service.UserService;

import com.fs.groupSeven.utils.MD5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

//IOC容器帮我们去new这个实现对象
//    IOC容器帮我们去new这个实现对象
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public Map<String, Object> login(String username, String password) {
        Map<String, Object> result = new HashMap<>();

        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
//            if (rememberMe != null && rememberMe.equals("1")){
//                token.setRememberMe(true);
//            }
            try {
                currentUser.login(token);
            } catch (UnknownAccountException e) {
                //shiro告诉我们查无此人
                result.put("code", -2);
                result.put("message", username + "，没有该用户");
                return result;
            } catch (IncorrectCredentialsException e) {
                //shiro告诉我们密码匹配错误
                result.put("code", -3);
                result.put("message", "密码输入错误");
                return result;
            } catch (AuthenticationException e) {
                //shiro告诉我们出现认证异常
                result.put("code", -1);
                result.put("message", "登录失败");
                return result;
            }
        }
        User user = (User) currentUser.getSession().getAttribute("user");
        for (Role role : user.getRoles()) {
            if (role.getName().equals("教务主任")) {
                result.put("code", 1);
            }
            if (role.getName().equals("班主任")) {
                result.put("code", 2);
            }
            if (role.getName().equals("任课教师")) {
                result.put("code", 3);
            }
        }
        result.put("user", user);
        return result;
    }

    @Override
    public User loginOut() {
        Subject currentUser = SecurityUtils.getSubject();
        User user = (User) currentUser.getSession().getAttribute("user");
        currentUser.logout();
        return user;
    }

    @Override
    public User getOneByUsername(String username) {
        return userDao.queryByUsername(username);
    }

    @Override
    public boolean modifyOne(User user) {
        String hashedPassword = MD5Util.md5Hash(user.getPassword(), user.getUsername());
        user.setPassword(hashedPassword);
        int result = userDao.updateByUser(user);

        return result > 0;
    }

    @Override
    public boolean addOne(User user) {
        String hashedPassword = MD5Util.md5Hash(user.getPassword(), user.getUsername());
        user.setPassword(hashedPassword);
        int result = userDao.insertByUser(user);
        User user1 = userDao.queryByUsername(user.getUsername());

        if (user.getRoles() != null) {
            //遍历用户的角色，以循环执行user_role表的插入动作
            for (Role role : user.getRoles()) {
                //构造UserRole对象
                UserRole userRole = new UserRole();
                userRole.setRoleId(role.getRoleId());
                userRole.setUserId(user1.getUserId());
                //让userRoleDao执行插入UserRole
                userRoleDao.insert(userRole);
            }
        }
        return result > 0;
    }
}
